﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FileCryt
{
    /// <summary>
    /// CreateLicence.xaml 的交互逻辑
    /// </summary>
    public partial class CreateLicence : Window
    {
        public CreateLicence()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            int count = 0;

            if (!int.TryParse(Txt_Count.Text, out count))
            {
                MessageBox.Show("许可数量必须是一个整数", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (this.tbServerHdsn.Text.Length < 1)
            {
                MessageBox.Show("请输入服务器序列号");
                return;
            }

            try
            {
                Comm.Security.ServiceLicense.LicenseDir = System.Environment.CurrentDirectory;
                if (Comm.Security.ServiceLicense.Create(int.Parse(Txt_Count.Text), this.tbServerHdsn.Text))
                {
                    MessageBox.Show("创建成功，许可数量：" + Txt_Count.Text, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("授权文件生成错误，错误原因：" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Btn_GetSn_Click(object sender, RoutedEventArgs e)
        {
            string[] Sn = Comm.HardDisk.HardDiskHelper.GetAllHardDiskSN().Split(',');

            if (Sn.Length == 0) return;

            Txt_Sn.Text = Sn[0].Trim();
        }

        private void Btn_ClientCreate_Click(object sender, RoutedEventArgs e)
        {
            int count = 0;

            if (!int.TryParse(Txt_DayCount.Text, out count))
            {
                MessageBox.Show("许可天数必须是一个整数", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            int yxq = 0;

            if (!int.TryParse(Txt_FileYxq.Text, out yxq))
            {
                MessageBox.Show("许可证有效期必须是一个整数", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (Txt_Sn.Text.Trim() == "")
            {
                MessageBox.Show("序列号不能为空", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!Comm.Security.ClientLicense.Create(count, yxq, Txt_Sn.Text.Trim()))
            {
                MessageBox.Show("许可文件创建失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            MessageBox.Show("许可文件创建成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void Btn_ServerCreate_Click(object sender, RoutedEventArgs e)
        {
            int count = 0;

            if (!int.TryParse(Txt_ClientCount.Text, out count))
            {
                MessageBox.Show("许可数量必须是一个整数", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            int dayscount = 0;

            if (!int.TryParse(Txt_Days.Text, out dayscount))
            {
                MessageBox.Show("许可天数必须是一个整数", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            int yxq = 0;

            if (!int.TryParse(Txt_LicenseYxq.Text, out yxq))
            {
                MessageBox.Show("许可证有效期必须是一个整数", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }


            if (!Comm.Security.ServerLicense.CreateServerLicense(count, dayscount, yxq))
            {
                MessageBox.Show("许可文件创建失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            MessageBox.Show("许可文件创建成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void btnGetServerHdsn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.tbServerHdsn.Text = Comm.HardDisk.HardDiskHelper.GetAllHardDiskSN();
            }
            catch (Exception)
            {

            }
        }

    }
}
